

#include<stdio.h>
#include <string.h>
#include "main.h"


#include "BC26.h"
#include "Network_communication.h"
#include "TelecomPlatform.h"

#include "gpio.h"
#include "usart.h"
#include "flash.h"

extern eDeviceMode gDeviceRunMode;

extern uint8_t Sent_Data[66];
extern uint8_t Sample_Data[8];
extern uint16_t Battary_Val;
extern uint16_t adc_calt_value;
extern uint8_t SentTimes;
extern uint8_t Bc26_CESQ;

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];
extern uint8_t Rece_len;
extern uint8_t Recebuffer[ReceiveUdpDataMaxLen];
/***
����һ��������Ҫ4s
***/
/* uint8_t	SendMessageToServer(measure_value*value)
{
	
	    uint8_t i = 0, errcode,j=0;
	    int16_t	TempValue=0;
	
	
		  uint8_t sendbuffer[IMEI_Len+ICCID_Len+12*2+1] = {
							 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						   0x0b,			       
						   0x00, 0X00,		   
						   0X01,			       
						   0x00,			       
						   0x00, 0x00,       
				       0x00, 0x00,       
						   0x00,			       
						   0x11, 0x22,		   
						   0};
	    uint8_t databuffer[50]={0};
	
      sendbuffer[IMEI_Len+ICCID_Len+4]=value->AlarmFlag;
		
				
				
				
				

				
				
				
				TempValue=(value->Bat_Vdd_Val*value->Water_PressureVal)/4095;
				
				TempValue=TempValue*3125/1000-1250;
				
				if(TempValue<0)
				{
					TempValue=0;
				}
			
			
			if((gDeviceRunMode==Fire_Hydrant_Mode)||(gDeviceRunMode==Water_Pressure_Mode))
			{
						if(gDeviceRunMode==Fire_Hydrant_Mode)
						{
									sendbuffer[IMEI_Len+ICCID_Len+1] =0xF4;
									sendbuffer[IMEI_Len+ICCID_Len+2] =0xF4;
						}
						else if(gDeviceRunMode==Water_Pressure_Mode)
						{
									sendbuffer[IMEI_Len+ICCID_Len+1] =0xF3;
									sendbuffer[IMEI_Len+ICCID_Len+2] =0xF3;
						}

						sendbuffer[IMEI_Len+ICCID_Len+5] =(TempValue&0xFF00)>>8;
						sendbuffer[IMEI_Len+ICCID_Len+6] =(TempValue&0x00FF)>>0;
						
					  sendbuffer[IMEI_Len+ICCID_Len+7]=(value->Bat_Vdd_Val&0xFF00)>>8;
	          sendbuffer[IMEI_Len+ICCID_Len+8]=(value->Bat_Vdd_Val&0x00FF)>>0;	
						
            sendbuffer[IMEI_Len+ICCID_Len+9]=value->SignalStrength;

			}				
			else if(gDeviceRunMode==Water_Level_Mode)
			{
				  sendbuffer[IMEI_Len+ICCID_Len+1] =0xF2;
				  sendbuffer[IMEI_Len+ICCID_Len+2] =0xF2;
				
				
					sendbuffer[IMEI_Len+ICCID_Len+5] =(value->Water_LevelVal & 0xFF00) >> 8;
					sendbuffer[IMEI_Len+ICCID_Len+6] =value->Water_LevelVal & 0x00FF;
				
				  sendbuffer[IMEI_Len+ICCID_Len+7]=(value->Bat_Vdd_Val&0xFF00)>>8;
	        sendbuffer[IMEI_Len+ICCID_Len+8]=(value->Bat_Vdd_Val&0x00FF)>>0;	

				  sendbuffer[IMEI_Len+ICCID_Len+9]=value->SignalStrength;


			}
			else if(gDeviceRunMode==Door_Sensor_Mode)
			{
			    sendbuffer[IMEI_Len+ICCID_Len+1] =0xF5;
				  sendbuffer[IMEI_Len+ICCID_Len+2] =0xF5;
				
					sendbuffer[IMEI_Len+ICCID_Len+5] =value->door_sensor1_sw;
					sendbuffer[IMEI_Len+ICCID_Len+6] =value->door_sensor2_sw;
				
				  sendbuffer[IMEI_Len+ICCID_Len+7]=(value->Bat_Vdd_Val&0xFF00)>>8;
	        sendbuffer[IMEI_Len+ICCID_Len+8]=(value->Bat_Vdd_Val&0x00FF)>>0;	

			    sendbuffer[IMEI_Len+ICCID_Len+9]=value->SignalStrength;
			
			}
			else if(gDeviceRunMode==Water_Dip_Mode)
			{
			    sendbuffer[IMEI_Len+ICCID_Len+1] =0xF6;
				  sendbuffer[IMEI_Len+ICCID_Len+2] =0xF6;
				  sendbuffer[IMEI_Len+ICCID_Len+5] =value->water_dip_sw;  
				
				  sendbuffer[IMEI_Len+ICCID_Len+6]=(value->Bat_Vdd_Val&0xFF00)>>8;
	        sendbuffer[IMEI_Len+ICCID_Len+7]=(value->Bat_Vdd_Val&0x00FF)>>0;	

          sendbuffer[IMEI_Len+ICCID_Len+8]=value->SignalStrength;

			}
			
	
			sendbuffer[IMEI_Len+ICCID_Len+10]=0x11;     
			sendbuffer[IMEI_Len+ICCID_Len+11]=0x22;     
			

			j=0;
			for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+12;i++)
			{
				sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
				++j;
			}
		
	
			memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
			memcpy(sendbuffer+IMEI_Len,BC26_ICCID,ICCID_Len);

			memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,12*2);
			sendbuffer[IMEI_Len+ICCID_Len+12*2]=0;
			

		
			i = 0;
			while (++i < 3) 
			{
					errcode = SendDataToTelecomPlatform(sendbuffer,sizeof(sendbuffer)-1);
					if (errcode == TRUE)
					{
					

						  return	TRUE;


					}
					else
					{
						
						HAL_Delay(4000);
					}
			}

			return	FALSE;

		
} */





uint8_t	SendMessage_To_Server(void)
{
	uint8_t errcode;
	uint8_t i = 0;
	uint8_t j = 0;
	uint8_t sendbuffer[IMEI_Len+ICCID_Len+12*2+1];
	uint8_t databuffer[50]={0};
	
	Sample_Data[1] = Battary_Val;
	Sample_Data[0] = Battary_Val>>8;
	Sample_Data[3] = adc_calt_value;
	Sample_Data[2] = adc_calt_value>>8;
	Sample_Data[4] = Bc26_CESQ;
	Sample_Data[5] = SentTimes;
	Sample_Data[6] = 0x00; 
	Sample_Data[7] = 0xAA; 
	
	for(i=0;i<8;i++)
	{
		sprintf(Sent_Data+48+j*2,"%02x",Sample_Data[i]);
		++j;
	}
	Sent_Data[64] = 0;
	i = 0;
	while (++i < 2) 
	{
		
		errcode = SendDataToTelecomPlatform(Sent_Data,64);
		if (errcode == TRUE)
		{
			UART1_Rx_cnt = 0;
			return	TRUE;

		}
		else
		{
			HAL_Delay(4000);
		}
	}
	UART1_Rx_cnt = 0;
	return	FALSE;
	
}

void Receive_Message_From_Server(void)
{
	uint8_t i,j;
	uint8_t	buffer[ReceiveUdpDataMaxLen]={0},len;
	for(j=0;j<ReceiveUdpDataMaxLen;j++)
		Recebuffer[j] = 0;
	Rece_len = 0;
	
	len=ReceiveMessageFromTelecomPlatform(buffer);
	
	for(j=0;j<len;j++)
		Recebuffer[j] = buffer[j];
	Rece_len = len;
	UART1_Rx_cnt = 0;












}

uint8_t	SendAckToServer(void)
{
	uint8_t i = 0,j=0, errcode;
	
	uint8_t sendbuffer[IMEI_Len+ICCID_Len+10*2+1] = {
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	
						  0x09,
						  0xC1, 0XC1,
						  0X05, 
						  0x00,0x00,
						  0x00,0x00,
						  0x00,0x00,
						  0x00};
	uint8_t databuffer[50]={0};	
	





	
	
	
	
	
	
	
	
	



	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+10;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	
	memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,BC26_ICCID,ICCID_Len);
	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,10*2);
	sendbuffer[IMEI_Len+ICCID_Len+10*2]=0;
	
	

	i = 0;
	while (++i < 3) 
	{
		
		errcode = SendDataToTelecomPlatform(sendbuffer, sizeof(sendbuffer)-1);
		if (errcode == TRUE)
		{
			return	TRUE;

		}
		else
		{
			HAL_Delay(3000);

		}
	}

	return	FALSE;


}


void	CMDProcess(uint8_t *buf,uint8_t len)
{

	if(Write_Config(buf,len)==1)
	{
		HAL_NVIC_SystemReset();
	}

}

